package com.cykj.user.controller;

import com.cykj.common.core.web.controller.BaseController;
import com.cykj.common.core.web.domain.AjaxResult;
import com.cykj.common.core.web.page.TableDataInfo;
import com.cykj.common.log.annotation.Log;
import com.cykj.common.log.enums.BusinessType;
import com.cykj.common.security.annotation.RequiresPermissions;
import com.cykj.user.domain.CtBan;
import com.cykj.user.domain.Customer;
import com.cykj.user.service.ICtService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理控制器
 *
 * @author 阿强
 * @date 2025-08-18
 */
@Tag(name = "用户管理接口", description = "用户列表查询及黑名单管理")
@RestController
@RequestMapping("/ct")
public class CtController extends BaseController {  // 继承BaseController获取分页工具

    @Autowired
    private ICtService ctService;

    /**将用户加入黑名*/
    @Operation(summary = "加入黑名单", description = "将指定用户加入黑名单并记录封禁原因")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:user:blacklist")
    @PostMapping("/addToBlacklist")
    public AjaxResult addToBlacklist(@RequestBody CtBan ctBan) {
        // 调用服务层方法执行拉黑操作
        int result = ctService.addUserToBlacklist(ctBan);
        return result == 1
                ? AjaxResult.success("用户已成功加入黑名单")
                : AjaxResult.error("用户加入黑名单失败");
    }
    @Operation(summary = "批量解除黑名单", description = "将多个用户从黑名单中移除")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:user:blacklist")
    @PostMapping("/batchRemoveFromBlacklist")
    public AjaxResult batchRemoveFromBlacklist(@RequestBody List<Long> userIds) {
        int successCount = 0;
        for (Long userId : userIds) {
            successCount += ctService.removeUserFromBlacklist(userId);
        }
        return successCount > 0
                ? AjaxResult.success("成功解除 " + successCount + " 个用户的拉黑状态")
                : AjaxResult.error("批量解除拉黑失败");
    }




    /**查询用户列表*/
    @Operation(summary = "查询用户列表", description = "分页查询用户信息，支持条件筛选")
    @RequiresPermissions("system:user:list")
    @GetMapping("/list")
    public TableDataInfo list(Customer ct) {
        // 启动分页（BaseController提供的分页工具）
        startPage();
        // 调用服务层查询用户列表
        List<Customer> list = ctService.selectCtList(ct);
        // 封装分页结果并返回
        return getDataTable(list);
    }

    /**根据ID查询用户详情*/
    @Operation(summary = "查询用户详情", description = "根据ID查询用户详细信息")
    @RequiresPermissions("system:user:query")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ctService.selectCtById(id));
    }

    /*** 新增用*/
    @Operation(summary = "新增用户", description = "添加新用户信息")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:user:add")
    @PostMapping
    public AjaxResult add(@RequestBody Customer ct) {
        return toAjax(ctService.insertCt(ct));
    }

    /**修改用户信息*/
    @Operation(summary = "修改用户", description = "更新用户基本信息")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:user:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody Customer ct) {
        return toAjax(ctService.updateCt(ct));
    }
    @Operation(summary = "解除黑名单", description = "将指定用户从黑名单中移除")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:user:blacklist")
    @PostMapping("/removeFromBlacklist")
    public AjaxResult removeFromBlacklist(@RequestParam Long userId) {
        int result = ctService.removeUserFromBlacklist(userId);
        return result > 0
                ? AjaxResult.success("用户已成功移出黑名单")
                : AjaxResult.error("用户移出黑名单失败");
    }
}